﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;

namespace DLV.BackEnd.Pages
{
    public partial class EditVisaType : AdminPageBase
    {
        private VisaTypeInfo _visaTypeInfo = new VisaTypeInfo();
        private readonly VisaTypeController _visaTypeController = new VisaTypeController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["VisaTypeId"]))
                _visaTypeInfo = _visaTypeController.GetVisaType(int.Parse(Request.QueryString["VisaTypeId"]));

            if (Page.IsPostBack) return;

            LoadStatus();
            LoadUser();

            if (_visaTypeInfo.VisaTypeId == -1) return;

            RenderForm();
        }
        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListVisaType.aspx?xml=VisaType");
        }
        private void SaveData()
        {
            _visaTypeInfo.VisaTypeName = txtVisaTypeName.Text;
            _visaTypeInfo.Status = Convert.ToInt32(ddlStatus.SelectedValue);

            if (_visaTypeInfo.VisaTypeId == -1)
            {
                _visaTypeInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _visaTypeInfo.CreatedDate = DateTime.Now;
                _visaTypeController.InsertVisaType(_visaTypeInfo);
            }
            else
            {
                _visaTypeInfo.UpdatedBy = UserId;
                _visaTypeInfo.UpdatedDate = DateTime.Now;
                _visaTypeController.UpdateVisaType(_visaTypeInfo);
            }

            Response.Redirect("/Pages/ListVisaType.aspx?xml=VisaType");
        }

        private void RenderForm()
        {
            txtVisaTypeName.Text = _visaTypeInfo.VisaTypeName;
            ddlStatus.SelectedValue = _visaTypeInfo.Status.ToString();
            ddlCreatedBy.SelectedValue = _visaTypeInfo.CreatedBy.ToString();
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người thêm ---", "-1"));
        }
    }
}